package xcmg.device.web.setting;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import xcmg.device.infra.AuthCode;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.setting.AppSettingService;
import xcmg.device.service.vo.setting.AppSettingVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

/**
 * Created by sqq on 2018/3/14.
 */
@RequestMapping("/appSetting")
@RestController
public class AppSettingController extends BasicController {
    @Autowired
    private AppSettingService appSettingService;

    @ResponseBody
    @PostMapping(value = "/insert")
    @FastMappingInfo(needLogin = true, code = AuthCode.SystemManage.APP_SETTING_ADD)
    public ActionResult<String> insert(@RequestBody AppSettingVO appSettingVO) {
        if (StringUtils.isEmpty(appSettingVO.getFileName())) {
            return actionResult(ErrorCode.IllegalArument);
        }
        if (null == appSettingVO.getIsApproval()) {
            appSettingVO.setIsApproval(0);
        }
        if (!appSettingVO.getFileName().trim().endsWith(".apk")) {
            return actionResult(ErrorCode.ImportExcelEndWithAPK);
        } else {
            ErrorCode code = appSettingService.insert(appSettingVO);
            return actionResult(code);
        }
    }


    @GetMapping(value = "/item")
    @FastMappingInfo(needLogin = true)
    public ActionResult<AppSettingVO> item(String id){
        AppSettingVO appSettingVO=appSettingService.item(id);
        return actionValueResult(appSettingVO);
    }

    @GetMapping(value = "/itemByType")
    @FastMappingInfo(needLogin = false)
    public ActionResult<AppSettingVO> itemByType(){
        AppSettingVO appSettingVO=appSettingService.itemByType();
        return actionValueResult(appSettingVO);
    }

    @PostMapping(value = "/update")
    @FastMappingInfo(needLogin = true,code = AuthCode.SystemManage.APP_SETTING_EDIT)
    public ActionResult<String> update(@RequestBody AppSettingVO appSettingVO){
        ErrorCode code=appSettingService.update(appSettingVO);
        return actionResult(code);
    }

    @PostMapping(value = "/list")
    @FastMappingInfo(needLogin = true)
    public ActionResult<PageInfo<AppSettingVO>> list(@RequestBody SearchCommonVO<AppSettingVO> condition){
        PageCommonVO<AppSettingVO> pageCommonVO=appSettingService.list(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @GetMapping(value = "/download/{fileName}")
    public void downloadx(@PathVariable("fileName") String fileName) throws Exception {
        if (!fileName.endsWith(".apk")){
            fileName = fileName + ".apk";
        }
        appSettingService.download(fileName);
    }

    @GetMapping(value = "/itemLatestByType")
    @FastMappingInfo(needLogin = false)
    public ActionResult<AppSettingVO> itemLatestByType(String type){
        AppSettingVO appSettingVO=appSettingService.itemLatestByType(type);
        return actionValueResult(appSettingVO);
    }

}
